//
//  MainViewController.swift
//  weibo
//
//  Created by Amisear on 15/9/21.
//  Copyright © 2015年 Amisear. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    
    
    @objc private func addbtnDidClick() {
    
        print("中间按钮点击成功!")
    
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        addChildViewController()
        
        addButton()
        
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        
        tabBar.bringSubviewToFront(addBtn)
        
    }
    
    private func addChildViewController() {
        
        addChildViewController(HomeTableViewController(), imageNmae: "tabbar_home", title: "首页")
        addChildViewController(MessageTableViewController(), imageNmae: "tabbar_message_center", title: "消息")
        addChildViewController(HomeTableViewController(), imageNmae: "", title: "")
        addChildViewController(DiscoverTableViewController(), imageNmae: "tabbar_discover", title: "发现")
        addChildViewController(DiscoverTableViewController(), imageNmae: "tabbar_profile", title: "个人")
        
    }
    
    private func addChildViewController(vc: UIViewController ,imageNmae:String ,title:String) {
        
        tabBar.tintColor = UIColor.orangeColor()
        
        let vc = vc
        
        vc.tabBarItem.image = UIImage(named:imageNmae)
        
        vc.title = title
        
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
        
    }
    /**
    添加中间按钮 设置位置
    */
    // MARK: - 添加中间按钮
    private func addButton() {
        
        tabBar.addSubview(addBtn)
        
        tabBar.bringSubviewToFront(addBtn)
        
 /// 设置位置
        let count = self.childViewControllers.count
        
        let w = tabBar.bounds.width / CGFloat(count) - 1
        
        addBtn.frame = CGRectInset(tabBar.bounds, 2 * w, 0)
        
        addBtn.addTarget(self, action: "addbtnDidClick", forControlEvents: .TouchUpInside)
    }
    // MARK: - 懒加载
    private lazy var addBtn: UIButton = UIButton(imageName: "tabbar_compose_icon_add", backgrouundName: "tabbar_compose_button")
    
}









